﻿Imports System.Windows.Forms.Design
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.ComponentModel.Design

Namespace Controls
    Friend Class TabEditor
        Inherits WindowsFormsComponentEditor
        Public Overloads Overrides Function EditComponent(ByVal context As ITypeDescriptorContext, ByVal component As Object, ByVal owner As IWin32Window) As Boolean
            Dim oControl As TabControl = DirectCast(component, TabControl)
            Dim site As IServiceProvider = oControl.Site
            Dim changeService As IComponentChangeService = Nothing

            Dim transaction As DesignerTransaction = Nothing
            Dim changed As Boolean = False

            Try
                If site IsNot Nothing Then
                    Dim designerHost As IDesignerHost = DirectCast(site.GetService(GetType(IDesignerHost)), IDesignerHost)
                    transaction = designerHost.CreateTransaction("BuildTabStrip")

                    changeService = DirectCast(site.GetService(GetType(IComponentChangeService)), IComponentChangeService)
                    If changeService IsNot Nothing Then
                        Try
                            changeService.OnComponentChanging(component, Nothing)
                        Catch ex As CheckoutException
                            If ex Is CheckoutException.Canceled Then
                                Return False
                            End If
                            Throw ex
                        End Try
                    End If
                End If

                Try
                    Dim oEditorForm As New TabEditorForm(oControl)
                    If oEditorForm.ShowDialog(owner) = DialogResult.OK Then
                        changed = True
                    End If
                Finally
                    If changed AndAlso changeService IsNot Nothing Then
                        changeService.OnComponentChanged(oControl, Nothing, Nothing, Nothing)
                    End If
                End Try
            Finally
                If transaction IsNot Nothing Then
                    If changed Then
                        transaction.Commit()
                    Else
                        transaction.Cancel()
                    End If
                End If
            End Try

            Return changed
        End Function
    End Class
End Namespace